package com.sx.mvvmlibs.base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * @Author: YY
 * @Date: 2022/1/8
 * @Describe: DataBinding的Activity基类
 */
abstract class DataBindingBaseActivity<V: ViewDataBinding, VM: BaseViewModel<out BaseModel>>(
    @LayoutRes private val layoutId : Int,
    private val varViewModelId: Int? = null
) : ViewBindingBaseActivity<V, VM>() {

    final override fun initBinding(inflater: LayoutInflater, container: ViewGroup?): V =
        DataBindingUtil.inflate(inflater, layoutId, container, false)

    override fun initViewModel() {
        super.initViewModel()
        if(varViewModelId != null) {
            mBinding.setVariable(varViewModelId, mViewModel)
        }
        mBinding.lifecycleOwner = this
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.unbind()
    }

}